package com.hnu.tockmarket.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.hnu.tockmarket.R;
import com.hnu.tockmarket.fragment.HomeFragment;
import com.hnu.tockmarket.fragment.MarketFragment;
import com.hnu.tockmarket.fragment.NewFragment;
import com.hnu.tockmarket.fragment.UserFragment;

/**
 * 系统主页对应的活动
 *
 * @author hnulht
 * @since 2021-07-07
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity"; // 日志
    private SystemFragmentPagerAdapter adapter; // 绑定ViewPager与Fragment的适配器
    private ViewPager viewPager; // 视图翻页工具
    private TabLayout tabLayout; // 页面切换指示器
    private int[] unSelectTabImageList = new int[]{R.mipmap.main_home, R.mipmap.main_market, R.mipmap.main_new, R.mipmap.main_user}; // 未选中的Tab图片列表
    private int[] selectTabImageList = new int[]{R.mipmap.main_home_press, R.mipmap.main_market_press, R.mipmap.main_new_press, R.mipmap.main_user_press}; // 选中的Tab图片列表
    private String[] tabTitleList = {"系统首页", "股票行情", "经济新闻", "个人中心"}; // Tab标题列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // 调用父类onCreate()方法
        setContentView(R.layout.activity_main); // 给系统主页活动加载对应的布局
        initView(); // 初始化视图
        initListener(); // 初始化事件监听器
    }

    /**
     * 初始化视图
     */
    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.main_vp); // 定位视图翻页工具
        tabLayout = (TabLayout) findViewById(R.id.main_tl); // 定位页面切换指示器
        // 使用适配器将ViewPager与Fragment绑定在一起
        adapter = new SystemFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        // 将TabLayout与ViewPager绑定
        tabLayout.setupWithViewPager(viewPager);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            tab.setCustomView(adapter.getView(i));
        }
    }

    /**
     * 初始化事件监听器
     */
    private void initListener() {
        // TabLayout切换时导航栏图片处理
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 选中图片操作
                View view = tab.getCustomView(); // 获取Tab视图
                ImageView imageView = view.findViewById(R.id.tab_item_iv); // 定位Tab图标
                TextView textView = view.findViewById(R.id.tab_item_tv); // 定位Tab标题
                String tabTitle = textView.getText().toString().trim();
                if (tabTitle.equals("系统首页")) {
                    imageView.setImageResource(selectTabImageList[0]);
                } else if (tabTitle.equals("股票行情")) {
                    imageView.setImageResource(selectTabImageList[1]);
                } else if (tabTitle.equals("经济新闻")) {
                    imageView.setImageResource(selectTabImageList[2]);
                } else if (tabTitle.equals("个人中心")) {
                    imageView.setImageResource(selectTabImageList[3]);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                // 未选中图片操作
                View view = tab.getCustomView(); // 获取Tab视图
                ImageView imageView = view.findViewById(R.id.tab_item_iv); // 定位Tab图标
                TextView textView = view.findViewById(R.id.tab_item_tv); // 定位Tab标题
                String tabTitle = textView.getText().toString();
                if (tabTitle.equals("系统首页")) {
                    imageView.setImageResource(unSelectTabImageList[0]);
                } else if (tabTitle.equals("股票行情")) {
                    imageView.setImageResource(unSelectTabImageList[1]);
                } else if (tabTitle.equals("经济新闻")) {
                    imageView.setImageResource(unSelectTabImageList[2]);
                } else if (tabTitle.equals("个人中心")) {
                    imageView.setImageResource(unSelectTabImageList[3]);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 绑定ViewPager与Fragment的适配器
     */
    public class SystemFragmentPagerAdapter extends FragmentPagerAdapter {

        public SystemFragmentPagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return new HomeFragment();
            } else if (position == 1) {
                return new MarketFragment();
            } else if (position == 2) {
                return new NewFragment();
            } else if (position == 3) {
                return new UserFragment();
            }
            return new HomeFragment(); // 未知状态直接返回首页
        }

        @Override
        public int getCount() {
            return tabTitleList.length;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitleList[position];
        }

        public View getView(int position) {
            View view = View.inflate(MainActivity.this, R.layout.tab_item, null);
            ImageView imageView = view.findViewById(R.id.tab_item_iv);
            TextView textView = view.findViewById(R.id.tab_item_tv);
            if (tabLayout.getTabAt(position).isSelected()) {
                imageView.setImageResource(selectTabImageList[position]);
            } else {
                imageView.setImageResource(unSelectTabImageList[position]);
            }
            textView.setText(tabTitleList[position]);
            textView.setTextColor(tabLayout.getTabTextColors());
            return view;
        }

    }

}